import React from "react";
import { HashRouter, Routes, Route, Navigate } from "react-router-dom";
import Login from "../views/login";
import NewsSandBox from "../views/sandbox";
import NewsVistorList from "../views/newsVistor/newsVistorList";
import Detail from "../views/newsVistor/detail";

export default function IndexRouter() {
  return (
    <HashRouter>
      {/* 精准匹配  匹配完第一个就停*/}
      <Routes>
        <Route path="/login" element={<Login />} />
        <Route path="/newsVistorList" element={<NewsVistorList />} />
        {/* 动态路由 */}
        <Route path="/detail/:id" element={<Detail />} />
        {/* 重定向Navigate 的时候需鉴权 有token加载NewsSandBox  没有token重定向到Login 三元表达式判断 */}
        <Route
          path="/*"
          element={
            localStorage.getItem("token") ? (
              <NewsSandBox></NewsSandBox>
            ) : (
              <Navigate to="/login" />
            )
          }
        />
      </Routes>
    </HashRouter>
  );
}
